package com.iflytek.aiui.demo.chat.repository.chat;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.Transformations;

import com.iflytek.aiui.demo.chat.common.Constant;
import com.iflytek.aiui.demo.chat.db.MessageDao;
import com.iflytek.aiui.demo.chat.utils.SemanticUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.inject.Singleton;

import io.reactivex.Completable;
import io.reactivex.schedulers.Schedulers;

import static com.iflytek.aiui.demo.chat.repository.chat.RawMessage.FromType.AIUI;
import static com.iflytek.aiui.demo.chat.repository.chat.RawMessage.MsgType.TEXT;

@Singleton
public class ChatRepo {
    //当前消息列表
    private LiveData<List<RawMessage>> mInteractMsg;
    private MessageDao mMessageDao;

    @Inject
    public ChatRepo(MessageDao dao) {
        mMessageDao = dao;
        mInteractMsg = mMessageDao.getAllMessage();
    }

    public LiveData<List<RawMessage>> getInteractMessages(){
        return Transformations.map(mInteractMsg, messages -> {
            if(messages.size() == 0) {
                StringBuilder hello = new StringBuilder();
                hello.append("你好，很高兴见到你\n\n");
                hello.append("你可以文本或者语音跟我对话，更多的功能在左上角的设置里进行探索吧");
                fakeAIUIResult(0, Constant.SERVICE_HELLO, hello.toString(), null, null);

            }
            return messages;
        });
    }

    /**
     *
     * 模拟消息列表中的结果信息
     * @param rc AIUI结果的rc字段
     * @param service AIUI结果的service字段
     * @param answer AIUI结果的answer
     * @param semantic AIUI结果的语义结构
     * @param mapData AIUI结果的信源数据
     * @return 构造的聊天消息
     */
    public RawMessage fakeAIUIResult(int rc, String service, String answer,
                                     Map<String, String> semantic, Map<String, String> mapData){
        RawMessage msg = new RawMessage(AIUI, TEXT,
                SemanticUtil.fakeSemanticResult(rc, service, answer, semantic, mapData).getBytes());
        addMessage(msg);
        return msg;
    }

    /**
     * 更新消息列表中的消息内容
     * @param message
     */
    public void updateMessage(final RawMessage message){
        if(message == null) return;
        Completable
                .complete()
                .observeOn(Schedulers.io())
                .subscribe(() -> mMessageDao.updateMessage(message));
    }

    /**
     * 更新消息的CacheContent，更新前比较与原始内容是否相符，避免覆写已更新消息的数据
     * @param message
     */
    public void updateCacheContent(final RawMessage message) {
        if(message == null) return;

        Completable
                .complete()
                .observeOn(Schedulers.io())
                .subscribe(() -> {
                    RawMessage current = mMessageDao.getMessage(message.timestamp);
                    if(Arrays.equals(current.msgData, message.msgData)) {
                        updateMessage(message);
                    }
                });
    }

    public void addMessage(final RawMessage msg){
        Completable
                .complete()
                .observeOn(Schedulers.io())
                .subscribe(() -> mMessageDao.addMessage(msg));
    }

}
